package persistent.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import basic.BaseDAO;
import model.TForumConent;
import model.VForumContent;
import persistent.dao.ForumContentDAO;

public class ForumContentDAOImpl implements ForumContentDAO {
	BaseDAO base=null;
	
	public ForumContentDAOImpl() {
		base=new BaseDAO();
	}
	@Override
	public int produceValidSortId(int forumid) {
		String sql ="select max(ordernum) as total from T_Forumcontent where forumid=?";
		Object para[]={forumid};
		ResultSet rs = base.select(sql, para);
		try {
			rs.next();
			int row =rs.getInt(1);
			return row; 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
		
		
	}

	@Override
	public int addContent(TForumConent fcontent) {
		String proname="up_addForumContent(?,?,?,?,?)";
		Object para[]={fcontent.getForumid(),fcontent.getPhotocontentid(),
				fcontent.getTextcontent(),fcontent.getOrdernum(),fcontent.isIsrecommand()};
		int row = (Integer)base.excuteSelectProduce(proname, para);
		if(row!=0){
			return row;
		}
		else {
			return 0;
		}
			
		
	}

	@Override
	public boolean deleteContent(int id) {
		String proname ="up_deleteForumContent(?)";
		Object para[]={id};
		int row = (Integer)base.excuteDeleteProduce(proname, para);
		if(row!=0){
			return true;
		}
		else {
			return false;
		}
		
	}

	@Override
	public boolean deleteContentsBelongForum(int forumid) {
		String sql ="update T_Forumcontent set isrecommand ='true' where forumid=?";
		Object para[]={forumid};
		int row = base.update(sql, para);
		if(row!=0){
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public boolean updateForumContent(TForumConent fcontent) {
		String proname="up_updateForumContent(?,?,?,?,?,?)";
		Object para[]={fcontent.getContentid(),fcontent.getForumid(),fcontent.getPhotocontentid(),
				fcontent.getTextcontent(),fcontent.getOrdernum(),fcontent.isIsrecommand()};
		int row = (Integer)base.excuteSelectProduce(proname, para);
		if(row!=0){
			return true;
		}
		else{
			return false;
		}
		
	}

	@Override
	public VForumContent getContnentById(int id) {
		String sql ="select * from V_ForumContent where contentid=?";
		Object para[]={id};
		ResultSet rs = base.select(sql, para);
		VForumContent vf =new VForumContent(rs);
		base.close();
		return vf;
	}

	@Override
	public TForumConent getTContnentById(int id) {
		String sql ="select * from T_ForumContent where contentid=?";
		Object para[]={id};
		ResultSet rs = base.select(sql, para);
		TForumConent Tf =new TForumConent(rs);
		base.close();
		return Tf;
	}

	@Override
	public List<VForumContent> getContentListByForumId(int forumid) {
		String sql ="select * from V_ForumContent where forumid=? order by ordernum asc";
		Object para[]={forumid};
		ResultSet rs = base.select(sql, para);
		List<VForumContent> list =VForumContent.toList(rs);
		base.close();
		return list;
	}

}
